package com.sss.gsb.orm.repository;

import java.text.MessageFormat;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.hibernate.Query;
import org.springframework.stereotype.Repository;

import com.sss.gsb.orm.model.Insurance;
import com.sss.gsb.orm.model.RecordStatus;
@Repository
public class InsuranceRepository extends BaseRepository<Insurance>
	implements IGSBRepository<Insurance>{

	@Override
	protected Class<Insurance> getPersistantClass() {
		return Insurance.class;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Insurance> findByCriteria(String idCard, String name,
			String surname, String cpk,String cps) throws Exception {
	
		String stm = MessageFormat.format(STM, getPersistantClass().getSimpleName());
		Map<String, Object> param = new Hashtable<String, Object>();
		param.put("rec", RecordStatus.N);
		
		if (!StringUtils.isBlank(idCard)) {
			stm = stm.concat("and m.customer.idcard = :idcard");
			param.put("idcard", idCard);
		}
		if (!StringUtils.isBlank(name)) {
			stm = stm.concat("and m.customer.name = :name");
			param.put("name", name);
		}
		if (!StringUtils.isBlank(surname)) {
			stm = stm.concat("and m.customer.surname = :surname");
			param.put("surname", surname);
		}
		
		/* cpk and cps are mached into fields (HTEACH, HMEM)*/
		if (!StringUtils.isBlank(cpk)) {
			stm = stm.concat("and m.customer.hTaech = :cpk ");
			param.put("cpk", cpk);
		}
		
		if (!StringUtils.isBlank(cps)) {
			stm = stm.concat("and m.customer.hMem = :cps ");
			param.put("cps", cps);
		}

		Query query = getCurrentSession().createQuery(stm);
		
		this.mapQueryParameter(param, query);


		return (List<Insurance>)query.list();
	}

}
